package com.zhangyang.esper.view.custom;

import com.alibaba.fastjson.JSONObject;
import com.espertech.esper.client.*;
import com.espertech.esper.client.scopetest.EPAssertionUtil;
import com.espertech.esper.client.scopetest.SupportUpdateListener;
import com.espertech.esper.client.time.CurrentTimeEvent;
import com.espertech.esper.client.util.DateTime;
import com.espertech.esper.metrics.instrumentation.InstrumentationHelper;
import com.zhangyang.esper.SupportConfigFactory;
import com.zhangyang.esper.support.bean.SupportBeanTimestamp;
import junit.framework.TestCase;

import java.util.Iterator;
import java.util.List;

/**
 * @author Dell
 * @Title:
 * @Description: 请写注释类
 * @date 2022/6/23
 */
public class TestTimeOrderCustom extends TestCase {
    private EPServiceProvider epService;
    private SupportUpdateListener listener;

    public void setUp() {
        listener = new SupportUpdateListener();
        Configuration configuration = SupportConfigFactory.getConfiguration();

        epService = EPServiceProviderManager.getDefaultProvider(configuration);
        epService.initialize();
        if (InstrumentationHelper.ENABLED) {
            InstrumentationHelper.startTest(epService, this.getClass(), getName());
        }
    }

    protected void tearDown() throws Exception {
        if (InstrumentationHelper.ENABLED) {
            InstrumentationHelper.endTest();
        }
        listener = null;
    }

    public void testTimeOrder() {
        epService.getEPAdministrator().getConfiguration().addEventType(SupportBeanTimestamp.class);
        long l = DateTime.parseDefaultMSec("2002-02-01T9:00:00.000");
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(l));
        EPStatement stmt = epService.getEPAdministrator().createEPL("select  istream  * from SupportBeanTimestamp.ext:time_order(timestamp, 1 week) ");
//        EPStatement stmt = epService.getEPAdministrator().createEPL("select (select rstream * from SupportBeanTimestamp.ext:time_order(timestamp, 1 week))  from pattern[timer:interval(1 week)] ");
        stmt.addListener(new UpdateListener() {

            @Override
            public void update(EventBean[] newEvents, EventBean[] oldEvents) {
                for (EventBean eventBean : newEvents) {
                    System.err.println(JSONObject.toJSONString(eventBean));
                }
            }
        });
        sendEvent("E1", DateTime.parseDefaultMSec("2002-02-01T9:00:00.000"));


//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-03-01T9:00:00.000") - 1));
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-04T23:59:59.000")));
        sendEvent("E2", DateTime.parseDefaultMSec("2002-02-02T23:59:59.000"));
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-06T23:59:59.000")));
        sendEvent("E2-2", DateTime.parseDefaultMSec("2002-02-06T23:59:59.000"));
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-06T23:59:59.000")));
        sendEvent("E2-3", DateTime.parseDefaultMSec("2002-02-06T23:59:59.000")+1);
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-06T23:59:59.000")));
        sendEvent("E2-4", DateTime.parseDefaultMSec("2002-02-06T23:59:59.000")-2);
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-06T23:59:59.000")));
        sendEvent("E2-5", DateTime.parseDefaultMSec("2002-02-06T23:59:59.000"));
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-06T23:59:59.000")));
        sendEvent("E2-6", DateTime.parseDefaultMSec("2002-02-06T23:59:59.000"));


//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-03-05T10:00:00.000" + 1)));
//        sendEvent("E3", DateTime.parseDefaultMSec("2002-03-06T10:00:00.000") + 1);
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-08T23:59:59.000")));
        sendEvent("E2-7", DateTime.parseDefaultMSec("2002-02-06T23:59:59.000") + 5);
        List<EventBean[]> andResetLastNewData1 = listener.getNewDataList();
//        List<EventBean[]> andResetLastNewData2 = listener.getNewDataList();
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-03-01T8:00:00.000")));
//        sendEvent("E4", DateTime.parseDefaultMSec("2002-03-01T8:30:00.000"));
//        List<EventBean[]> andResetLastNewData3 = listener.getNewDataList();
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-03-01T9:00:00.000") + 1));
        /***总结：1. rstream 会输出 E1 E2 ,但是E4不会输出（延迟到达的问题不会出现，设置）
         2. 给出的一个月，为啥到2002-02-01T9:00:00.000 至 2002-02-09T23:59:59.000 就无法输出了
         ***/
        Iterator<EventBean> iterator = stmt.iterator();
        EventBean[] eventBeans = EPAssertionUtil.iteratorToArray(iterator);
        List<EventBean[]> andResetLastNewData = listener.getNewDataList();
        List<EventBean[]> lastOldData = listener.getOldDataList();
//        for (EventBean[] andResetLastNewDatum : andResetLastNewData) {
//            for (EventBean eventBean : andResetLastNewDatum) {
//                System.err.println(JSONObject.toJSONString(eventBean));
//            }
//
//        }
    }
    public void testTimeOrder1() {
        epService.getEPAdministrator().getConfiguration().addEventType(SupportBeanTimestamp.class);

        epService.getEPRuntime().sendEvent(0);
        EPStatement stmt = epService.getEPAdministrator().createEPL("select  rstream    * from SupportBeanTimestamp.ext:time_order(timestamp, 5 second) group by id ");
//        EPStatement stmt = epService.getEPAdministrator().createEPL("select (select rstream * from SupportBeanTimestamp.ext:time_order(timestamp, 1 week))  from pattern[timer:interval(1 week)] ");
        stmt.addListener(new UpdateListener() {

            @Override
            public void update(EventBean[] newEvents, EventBean[] oldEvents) {
                for (EventBean eventBean : newEvents) {
                    System.err.println(JSONObject.toJSONString(eventBean));
                }
                System.err.println("===================================================");
            }
        });
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(1000));// 1900-6900
//        sendEvent("E2-3", 1000);
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(2000));// 0-800
//        sendEvent("E1", 2000);


//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-03-01T9:00:00.000") - 1));
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-04T23:59:59.000")));
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(1200));


        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(3000));
        sendEvent("E3",3000);
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(5000));
        sendEvent("E1",5000);
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(2000));
        sendEvent("E2",2000);
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(1000));
        sendEvent("E1", 1000);
        sendEvent("E2", 1000);
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(8000));
//        sendEvent("E8", 8000);
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(7000));
//        sendEvent("E7", 7000);
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-06T23:59:59.000")));
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(1100));

//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-06T23:59:59.000")));
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(2000));

//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-06T23:59:59.000")));
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(6000));

//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-06T23:59:59.000")));
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(3000));

//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-02-06T23:59:59.000")));
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(4000));


        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(6000));
        sendEvent("E6",6000);
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-03-05T10:00:00.000" + 1)));
//        sendEvent("E3", DateTime.parseDefaultMSec("2002-03-06T10:00:00.000") + 1);
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(8000-200));
        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(14000));
        sendEvent("E4", 14000);
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(9000));
//        sendEvent("E9", 9000);
//
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(12000));
//        sendEvent("E12", 12000);
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(11000));
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(11000-200));
//        sendEvent("E2-8", 11000);
        List<EventBean[]> andResetLastNewData1 = listener.getNewDataList();
//        List<EventBean[]> andResetLastNewData2 = listener.getNewDataList();
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-03-01T8:00:00.000")));
//        sendEvent("E4", DateTime.parseDefaultMSec("2002-03-01T8:30:00.000"));
//        List<EventBean[]> andResetLastNewData3 = listener.getNewDataList();
//        epService.getEPRuntime().sendEvent(new CurrentTimeEvent(DateTime.parseDefaultMSec("2002-03-01T9:00:00.000") + 1));
        /***总结：1. rstream 会输出 E1 E2 ,但是E4不会输出（延迟到达的问题不会出现，设置）
         2. 给出的一个月，为啥到2002-02-01T9:00:00.000 至 2002-02-09T23:59:59.000 就无法输出了
         ***/
        Iterator<EventBean> iterator = stmt.iterator();
        EventBean[] eventBeans = EPAssertionUtil.iteratorToArray(iterator);
        List<EventBean[]> andResetLastNewData = listener.getNewDataList();
        List<EventBean[]> lastOldData = listener.getOldDataList();

    }
    protected SupportBeanTimestamp sendEvent(String id, long timestamp) {
        SupportBeanTimestamp theEvent = new SupportBeanTimestamp(id, timestamp);
        EPRuntime runtime = epService.getEPRuntime();
        runtime.sendEvent(theEvent);
        return theEvent;
    }
}

